package com.controller.gzp;

import cn.dev33.satoken.exception.NotLoginException;
import cn.dev33.satoken.stp.StpUtil;
import com.service.gzp.TreeService;
import com.util.gzp.AjaxResponse;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.weaver.loadtime.Aj;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.constraints.NotNull;
import java.util.Arrays;

/**
 * ClassName: RoleTreeController
 * Package: com.controller.gzp
 *
 * @author: guanzepeng
 * @Date: 2021/4/10 15:16 星期六
 * @Description: 控制角色权限以及菜单的cotroller
 */
@RestController
@Validated
@Slf4j
@RequestMapping("/tree")
@Api(tags = "控制角色权限以及菜单的cotroller")
public class RoleTreeController {

	@Autowired
	private TreeService treeServiceImpl;

	/**
	 * 根据传入的角色ID查询权限ID
	 * @param rid 传入角色ID
	 * @return 返回数组,对应菜单ID
	 */
	@GetMapping("/getQuanXian")
	@ApiOperation(value = "根据传入的角色ID查询权限ID", response = AjaxResponse.class)
	@ApiImplicitParam(name = "rid", value = "传入角色ID", required = true, dataTypeClass = Integer.class)
	public AjaxResponse getQuanXian(@NotNull(message = "角色ID不能为空") Integer rid) {
		AjaxResponse quanXian = treeServiceImpl.getQuanXian(rid);
		return quanXian;
	}

	/**
	 * 查询当前用户所拥有的菜单ID
	 * @return 返回数组
	 */
	@PostMapping("/findByTree")
	@ApiOperation(value = "查询当前用户所拥有的菜单ID", response = AjaxResponse.class)
	public AjaxResponse findByTree() {
		Integer uid = null;
		try {
			uid = StpUtil.getLoginIdAsInt();
		} catch (NotLoginException e) {
			return AjaxResponse.success("请先进行登录!", 500);
		}
		AjaxResponse byTree = treeServiceImpl.findByTree(uid);
		return byTree;
	}

	/**
	 * 批量新增角色权限
	 * @param rid 角色ID
	 * @param tid 权限ID数组
	 * @return 返回成功失败
	 */
	@PostMapping("/updateQuanXian")
	@ApiOperation(value = "批量新增角色权限", response = AjaxResponse.class)
	@ApiImplicitParams({
			@ApiImplicitParam(name = "rid", value = "传入角色ID", required = true, dataTypeClass = Integer.class),
			@ApiImplicitParam(name = "tid", value = "权限ID数组", required = true, dataTypeClass = Arrays.class)
	})
	public AjaxResponse updateQuanXian(@NotNull(message = "角色ID不能为空") Integer rid, int[] tid) {
		AjaxResponse ajaxResponse = treeServiceImpl.updateQuanXian(rid, tid);
		return ajaxResponse;
	}
}
